package ledrpt

import com.tsqc.system.util.table.TranposUtils
import org.kdx.DateUtils

import java.text.SimpleDateFormat

class StepInOutYieldController {
    def dynamicQueryService
    def stepInOutYield() {
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()-30))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
      /*  GregorianCalendar gc2 = (GregorianCalendar) Calendar.getInstance();
        gc2.setTime(new Date());
        gc2.set(Calendar.DAY_OF_MONTH, 1);
        startTime = gc2.getTime()*/


        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)

        def dayList = DateUtils.getDayList(startTime, endTime)

        params.SYS_QUERY_NAME = 'stepInOutYield'
        def stepInOutYield = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'stepInOutYield2'
        def stepInOutYield2 = dynamicQueryService.queryForList(params)
        def title=['INOUTTIME']
        def groupId=['TRACK_IN_MAIN_QTY','TRACK_OUT_MAIN_QTY','YIELD','INOUTTIME']
        def stepInOutYield1= TranposUtils.groupTable(groupId,stepInOutYield,title)

        params.SYS_QUERY_NAME='packageForm'
        def packageForm=dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME='stepList'
        def stepList=dynamicQueryService.queryForList(params)

        def INData = []
        def OUTData = []
        def YIELDData = []
            for(def j=0;j<dayList.size();j++){
                def flag = false;
                for(def i=0;i<stepInOutYield.size();i++){
                    if(dayList[j]==stepInOutYield[i].INOUTTIME){
                        INData.push(stepInOutYield[i].TRACK_IN_MAIN_QTY)
                        OUTData.push(stepInOutYield[i].TRACK_OUT_MAIN_QTY)
                        YIELDData.push(stepInOutYield[i].YIELD)
                        flag = true;
                    }
                }
                if(!flag){
                    INData.push(0)
                    OUTData.push(0)
                    YIELDData.push(0)
                }
            }

        [stepInOutYield:stepInOutYield,dayList:dayList,stepInOutYield1:stepInOutYield1,
         stepInOutYield2:stepInOutYield2,
         startTime:startTime,endTime:endTime,
             INData:INData,OUTData:OUTData,YIELDData:YIELDData,
         LOT_ID:params.LOT_ID,PART_NAME:params.PART_NAME,SUBSTRATE_ID1:params.SUBSTRATE_ID1,RESERVED1:params.RESERVED1,stepList:stepList,packageForm:packageForm,
         CUSTOMER_ORDER:params.CUSTOMER_ORDER,startTime:params.startTime,STEP_NAME:params.STEP_NAME]
    }
    def exportExcel() {
        def sysQueryName

        sysQueryName = params.SYS_QUERY_NAME

        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
       /* GregorianCalendar gc2 = (GregorianCalendar) Calendar.getInstance();
        gc2.setTime(new Date());
        gc2.set(Calendar.DAY_OF_MONTH, 1);
        startTime = gc2.getTime()*/


        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)

        def dayList = DateUtils.getDayList(startTime, endTime)
        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        params.SYS_QUERY_NAME = 'stepInOutYield2'
        def stepInOutYield2 = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'stepInOutYield'
        def stepInOutYield = dynamicQueryService.queryForList(params)
        def title=['INOUTTIME']
        def groupId=['TRACK_IN_MAIN_QTY','TRACK_OUT_MAIN_QTY','YIELD','INOUTTIME']
        def stepInOutYield1= TranposUtils.groupTable(groupId,stepInOutYield,title)

        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList,
                                                                        stepInOutYield2:stepInOutYield2,
                                                                        stepInOutYield1:stepInOutYield1,dayList:dayList])
    }
}
